<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<%@ taglib prefix="fn" uri="/WEB-INF/tlds/fn.tld"%>
<%@ taglib prefix="fmt" uri="/WEB-INF/tlds/fmt.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>货品列表</title>
<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/jquery/jquery-1.4.4.min.js"	type="text/javascript"></script>
<script src="${BasePath}/res/js/common/utils.js" type="text/javascript"></script>
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.form.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.validate.method.js" type="text/javascript"></script>
<script src="${Plugins}/datepicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>
<style type="text/css">
#searchbar{
	margin-bottom: 10px;
}
.navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
.l-button-search{
	width: 50px;
	margin-left: 10px;
	padding-bottom: 2px;
	display: inline;
}
.l-button-test{width:80px; float:left; margin-left:10px; padding-bottom:2px;}
</style>
</head>
<body style="padding:6px; overflow:hidden;">
<div id="">
	<div class="navbar"><span>您当前的位置：基本信息管理 &gt;商品让利设置&gt;让利设置</span></div>
	<br/>
	<table width="100%" cellpadding="0" cellspacing="0" class="l-table-edit" style="TABLE-LAYOUT: fixed" >
	    <tr height="15">
	     <td align="right" class="l-table-edit-td">商品编码：</td>
	        <td align="left" class="l-table-edit-td">${CommodityVo1.commoNo}</td>
	        <td align="right" class="l-table-edit-td">商品名称：</td>
	        <td align="left" class="l-table-edit-td">${CommodityVo1.commoName}</td>
	       
	    </tr>
	    <tr height="15"></tr>
	</table>
	<br/>
</div>
<div id="maingrid6" style="margin:0; padding:0"></div>
  <div style="display:none;">
  <!-- g data total ttt -->
</div>
<form name="form1" method="post" action="c_profits.do" id="form1">
 	<input type=hidden id="code" name="code" value="${code}">
	<input type="hidden" id="commodityCode" name="commodityCode" value="${CommodityVo1.commoNo}">
	<input type="hidden" id="commodityName" name="commodityName" value="${CommodityVo1.commoName}">
	<input type="hidden" id="profitlist" name="profitlist" value="">
</form>
<script type="text/javascript">
    var basePath='${BasePath}';
	var grid6 = null;
	var rows = ${rows};
	var total = ${total};
    var profitsData = {"Rows": rows, "Total": total};
    var datalist = new Array();
    var prodNo=$("#prodNo").val();
    $(function () {
    	grid6 = $("#maingrid6").ligerGrid({
  	      columns: [
                    { display: '货品编码', name: 'prodNo', width: '10%', render: function(rowdata, rowindex, value) {
                    	return '<span title="' + value + '">' + value + '</span>';
                    } },
  	                { display: '商品名称', name: 'commoName', width: '10%', render: function(rowdata, rowindex, value){
  	                	return '<span title="${fn:escapeXml(CommodityVo1.commoName)}">${fn:escapeXml(CommodityVo1.commoName)}</span>';
  	                }},
  	                { display: '规格', width: '10%', name: 'casePakgSize' },
  	                { display: '品牌', name: 'brandName', width: '10%', render: function(rowdata, rowindex, value){
	                	return "${fn:escapeXml(CommodityVo1.brandName)}";
	                }},
	                { display: '分类', name: 'catName', width: '10%', render: function(rowdata, rowindex, value){
	                	return rowdata.catName;
	                }},
	                { display: '原成本', name: 'originalCost', width: '10%', type: 'float', editor: { type: 'float',minValue:0}, render: function(rowdata, rowindex, value){
	                	if(value > 0){      		
	                		value = String(value).substr(0,10);
	                	}
	                	return rowdata.originalCost = value;
	                }},
	                { display: '原零售价', name: 'originalRetailCost', width: '10%', type: 'float', editor: { type: 'float',minValue:0}, render: function(rowdata, rowindex, value){
	                	if(value > 0){
	                		value = String(value).substr(0,10);
	                	}
	                	return rowdata.originalRetailCost = value;
	                }},
	                { display: '原毛利(%)', name: 'originalRate', width: '10%', render: function(rowdata, rowindex, value){
	                	var value = '';
	                	if (typeof(rowdata.originalCost) != 'undefined' && !isNaN(rowdata.originalCost) && rowdata.originalCost > 0 && typeof(rowdata.originalRetailCost) != 'undefined' && !isNaN(rowdata.originalRetailCost) && rowdata.originalRetailCost > 0) {
	                		value = (((rowdata.originalRetailCost - rowdata.originalCost) / rowdata.originalRetailCost) * 100).toFixed(2);
	                	}
	                	return value;
	                }},
	                { display: '促销成本', name: 'promotionsCost', width: '10%', type: 'float', editor: { type: 'float',minValue:0}, render: function(rowdata, rowindex, value){
	                	if(value > 0){
	                		value = String(value).substr(0,10);
	                	}
	                	return rowdata.promotionsCost = value;
	                }},
	                { display: '促销零售价', name: 'promotionsRetailCost', width: '10%', type: 'float', editor: { type: 'float',minValue:0}, render: function(rowdata, rowindex, value){
	                	if(value > 0){
	                		value = String(value).substr(0,10);
	                	}
	                	return rowdata.promotionsRetailCost = value;
	                }},
	                { display: '促销毛利(%)', name: 'promotionsRate', width: '10%', render: function(rowdata, rowindex, value){
	                	var value = '';
	                	if (typeof(rowdata.promotionsCost) != 'undefined' && !isNaN(rowdata.promotionsCost) && rowdata.promotionsCost > 0 && typeof(rowdata.promotionsRetailCost) != 'undefined' && !isNaN(rowdata.promotionsRetailCost) && rowdata.promotionsRetailCost > 0) {
	                		value = (((rowdata.promotionsRetailCost - rowdata.promotionsCost) / rowdata.promotionsRetailCost) * 100).toFixed(2);
	                	}
	                	return value;
	                }},
	                { display: '当前库存数量', name: 'productQuantity', width: '10%' },
	                { display: '特价有效期', name: 'validTime', width: '20%', render: function(rowdata, rowindex, value) {
	                	var value = '';
	                		value += '<input type="hidden" id="tomorrow' + rowdata.__index + '" name="tomorrow" value="' + rowdata.tomorrow + '">';
	                		value += '<input id="validTimeStart' + rowdata.__index + '" type="text" name="validTimeStart" readonly="readonly" value="' + rowdata.validTimeStart + '" class="Wdate" value="" size="12" ';
	                		if (rowdata.enable == 1) {
	                			value += 'disabled="disabled" onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\',readOnly:true,autoPickDate:true,maxDate:\'#F{$dp.$D(\\\'validTimeEnd' + rowdata.__index + '\\\')||\\\'2020-10-01\\\'}\'})" >~';
	                		} else {
	                			value += 'onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\',readOnly:true,autoPickDate:true,minDate:\'#F{$dp.$D(\\\'tomorrow' + rowdata.__index + '\\\')||\\\'2013-01-01\\\'}\',maxDate:\'#F{$dp.$D(\\\'validTimeEnd' + rowdata.__index + '\\\')||\\\'2020-10-01\\\'}\'})" >~';
	                		}
	                    	value += '<input id="validTimeEnd' + rowdata.__index + '" type="text" name="validTimeEnd" readonly="readonly" value="' + rowdata.validTimeEnd + '" class="Wdate" value="" size="12"';
	                    	value += 'onFocus="WdatePicker({dateFmt:\'yyyy-MM-dd\',readOnly:true,autoPickDate:true,minDate:\'#F{$dp.$D(\\\'validTimeStart' + rowdata.__index + '\\\')||\\\'2013-01-01\\\'}\'})" >';
                    	return value;
	                } },
	                { display: '操作',width:'10%', isSort: false, render: function (rowdata, rowindex, value){
	                	var h="";
	                	 h += "<a href='javascript:showSetHistory(" + rowindex + ")'>查看设置历史</a> &nbsp;&nbsp;&nbsp;&nbsp;";
	                    return h;
	                	}
	                }
 	            ], 
               	width: '100%',
 	            height: '100%',
  	            enabledEdit: true,
  	            checkbox: true,
  	            data: $.extend(true,{},profitsData),
  	            selectRowButtonOnly: true,
  	            onBeforeEdit : function (obj) {
  	            	var enable = false;
  	            	if (obj.record.enable != 1) {
  	            		enable = true;
  	            	}
  	            	return enable;
  	            }, 
		        isChecked : function (rowdata){
		        	var selected = false;
		        	if (rowdata.selected == 1) {
		        		selected = true;
		        	}
		        	return selected;
		        },
  	          	toolbar: {
  	            	items: [
  	    					{ text: "提交", click: addProfits, icon: "add" },
  	    					{ line: true },
  	    					{ text: "取消", click: cancelSetProfits, icon: "prev" }
  	           	        ]
  	                },
  	          	onAfterEdit : function (obj) {
  	          		if (typeof(obj.record.originalCost) != 'undefined' && !isNaN(obj.record.originalCost) && typeof(obj.record.originalRetailCost) != 'undefined' && !isNaN(obj.record.originalRetailCost)) {
  	          			grid6.updateRow({
  	          				originalRate: (obj.record.originalRetailCost - obj.record.originalCost) / obj.record.originalCost
	  	                });
  	          		}
  	          		if (typeof(obj.record.promotionsCost) != 'undefined' && !isNaN(obj.record.promotionsCost) && typeof(obj.record.promotionsRetailCost) != 'undefined' && !isNaN(obj.record.promotionsRetailCost)) {
	          			grid6.updateRow({
	          				promotionsRate: (obj.record.promotionsRetailCost - obj.record.promotionsCost) / obj.record.promotionsCost
  	                	});
	          		}
  	          	},
  	         
  	          	onBeforeCheckRow: function (checked,rowdata,rowindex){
  	          		if (rowdata.enable == 1 && rowdata.selected == 1) {
  	          			$.ligerDialog.warn('特价有效期内且已选择的数据不能更改选择状态');
	          			return false;
  	          		}
  	          		if (typeof(rowdata.originalCost) == 'undefined' || isNaN(rowdata.originalCost) || rowdata.originalCost == '') {
  	          			$.ligerDialog.warn('请输入原成本');
  	          			return false;
  	          		}
  	          		if (typeof(rowdata.originalRetailCost) == 'undefined' || isNaN(rowdata.originalRetailCost) || rowdata.originalRetailCost == '') {
	          			$.ligerDialog.warn('请输入原零售价');
	          			return false;
	          		}
	  	          	if (typeof(rowdata.promotionsCost) == 'undefined' || isNaN(rowdata.promotionsCost) || rowdata.promotionsCost == '') {
	          			$.ligerDialog.warn('请输入促销成本');
	          			return false;
	          		}
	          		if (typeof(rowdata.promotionsRetailCost) == 'undefined' || isNaN(rowdata.promotionsRetailCost) || rowdata.promotionsRetailCost == '') {
	          			$.ligerDialog.warn('请输入促销零售价');
	          			return false;
	          		}
	          		if ($('#validTimeStart' + rowdata.__index).val() == '') {
	          			$.ligerDialog.warn('请输入特价有效期开始时间');
	          			rowdata.validTimeStart = '';
	          			return false;
	          		} else {
	          			rowdata.validTimeStart = $('#validTimeStart' + rowdata.__index).val();
	          		}
	          		if ($('#validTimeEnd' + rowdata.__index).val() == '') {
	          			$.ligerDialog.warn('请输入特价有效期结束时间');
	          			rowdata.validTimeEnd = '';
	          			return false;
	          		} else {
	          			rowdata.validTimeEnd = $('#validTimeEnd' + rowdata.__index).val();
	          		}
  	            },
  	         
  	            onBeforeCheckAllRow:function (checked, rowdata, rowindex){
  	            	for( var rowid in this.records){
  	            		if (this.records[rowid].enable == 1 && this.records[rowid].selected == 1) {
  	  	          			$.ligerDialog.warn('特价有效期内且已选择的数据不能更改选择状态');
  		          			return false;
  	  	          		}
  	            		if (typeof(this.records[rowid].originalCost) == 'undefined' || isNaN(this.records[rowid].originalCost) || this.records[rowid].originalCost == '') {
  	  	          			$.ligerDialog.warn('请输入原成本');
  	  	          			return false;
  	  	          		}
  	  	          		if (typeof(this.records[rowid].originalRetailCost) == 'undefined' || isNaN(this.records[rowid].originalRetailCost) || this.records[rowid].originalRetailCost == '') {
  		          			$.ligerDialog.warn('请输入原零售价');
  		          			return false;
  		          		}
  		  	          	if (typeof(this.records[rowid].promotionsCost) == 'undefined' || isNaN(this.records[rowid].promotionsCost) || this.records[rowid].promotionsCost == '') {
  		          			$.ligerDialog.warn('请输入促销成本');
  		          			return false;
  		          		}
  		          		if (typeof(this.records[rowid].promotionsRetailCost) == 'undefined' || isNaN(this.records[rowid].promotionsRetailCost) || this.records[rowid].promotionsRetailCost == '') {
  		          			$.ligerDialog.warn('请输入促销零售价');
  		          			return false;
  		          		}
  		          		if ($('#validTimeStart' + this.records[rowid].__index).val() == '') {
  		          			$.ligerDialog.warn('请输入特价有效期开始时间');
  		          			this.records[rowid].validTimeStart = '';
  		          			return false;
  		          		} else {
  		          			this.records[rowid].validTimeStart = $('#validTimeStart' + this.records[rowid].__index).val();
  		          		}
  		          		if ($('#validTimeEnd' + this.records[rowid].__index).val() == '') {
  		          			$.ligerDialog.warn('请输入特价有效期结束时间');
  		          			this.records[rowid].validTimeEnd = '';
  		          			return false;
  		          		} else {
  		          			this.records[rowid].validTimeEnd = $('#validTimeEnd' + this.records[rowid].__index).val();
  		          		}
  	            	}
  	            }
  		});
    });
    
	function addProfits() {
		var rows = grid6.rows;
		if (rows.length > 0) {
			for (var i=0; i<rows.length; i++) {
				rows[i].validTimeStart = $('#validTimeStart' + rows[i].__index).val();
				rows[i].validTimeEnd = $('#validTimeEnd' + rows[i].__index).val();
			}
		}
		var objs = grid6.getSelectedRows();
		if (objs.length == 0) {
			$.ligerDialog.warn('请选择至少一条数据');
			return;
		}
		var obj = null;
		for (var i=0; i<objs.length; i++) {
			if (objs[i].originalCost == '' || objs[i].originalRetailCost == '' ||
					objs[i].promotionsCost == '' || objs[i].promotionsRetailCost == '' ||
					objs[i].validTimeEnd == '' || objs[i].validTimeStart == '') {
				$.ligerDialog.warn('您选择的数据不能有空值');
				return;
			}
			if (objs[i].validTimeEnd < objs[i].validTimeStart) {
				$.ligerDialog.warn('特价有效期有数据设置不正确（开始有效期应小于结束有效期）');
				return;
			}
			obj = new Object();
			obj.brandName = objs[i].brandName;
			obj.brandNo = objs[i].brandNo;
			obj.casePakgSize = objs[i].casePakgSize;
			obj.catName = objs[i].catName;
			obj.catNo = objs[i].catNo;
			obj.commoName = objs[i].commoName;
			obj.commoNo = objs[i].commoNo;
			obj.inventoryType = objs[i].inventoryType;
			obj.originalCost = objs[i].originalCost;
			obj.originalRetailCost = objs[i].originalRetailCost;
			obj.prodBarCode = objs[i].prodBarCode;
			obj.prodNo = objs[i].prodNo;
			obj.promotionsCost = objs[i].promotionsCost;
			obj.promotionsRetailCost = objs[i].promotionsRetailCost;
			obj.salePrice = objs[i].salePrice;
			obj.saleType = objs[i].saleType;
			obj.validTimeEnd = objs[i].validTimeEnd;
			obj.validTimeStart = objs[i].validTimeStart;
			obj.enable = objs[i].enable;
			datalist.push(obj);
		}
		$('#profitlist').val(JSON.stringify(datalist));
		datalist = [];
        var options = {
        		type: 'POST',
        		success: function(data){
       			    if(data=="success") {
       			    	alert('提交成功');
       			    	close_liger_tab();
          	        } else {
          	          	window.parent.$.ligerDialog.error("新增失败");
          	   		}
        		}
        	}, $form1 = $("#form1");
        $form1.ajaxSubmit(options);
        return false;
	}
	
	function cancelSetProfits() {
		close_liger_tab();
	}
	
	function showSetHistory(rowindex) {
		var productCode = grid6.getData()[rowindex].prodNo;
		var code = $('#code').val();
		var m=$.ligerDialog.open({title:'查看设置历史',url:basePath+'/base/ProfitsController/showSetHistory.do?productCode=' + productCode + '&code=' + code,height:450,width:1000,isResize: false, isDrag: false ,
	  		buttons: [
	        { text: '返回', onclick: function (item, dialog) { 
	    	    dialog.close(); } 
		    }]
		});
  	    m.show();
	}
    
</script>


</body>
</html>